const Controller = require('egg').Controller;
class ssh extends Controller{
    async receive(){
        const {ctx,app}=this;
        await ctx.socket.emit('res', "hello");
    };
    async openSSH(){
        let self = this;
        const params = {
            host:'180.76.184.253',
            port:22,
            username:'root',
            password:'@#wang839305939'
         };
        const client = await this.service.ssh.createClient(params);
        if(client){
             const cmd="ls -la";
             client.exec(cmd,async (err, stream)=>{
                if(err){
                    return;
                } 
                stream.on('close',async ()=>{
                  //await self.ctx.socket.emit("res","\n\r");
                  client.end();
                }).on('data',async (data)=>{
                  await self.ctx.socket.emit("res",data);
                }).stderr.on('data',async ()=>{
                });
                stream.end('ls -l\nexit\n');
            });
            
        }else{
             this.ctx.socket.emit("openSSH",false);
        } 
    }
    async hostname(){
        let self = this;
        const params = {
            host:'180.76.184.253',
            port:22,
            username:'root',
            password:''
         };
        const client = await this.service.ssh.createClient(params);
        if(client){
             const cmd="uname -n";
             client.exec(cmd,async (err, stream)=>{
                if(err){
                    return;
                } 
                stream.on('close',async ()=>{
                  client.end();
                }).on('data',async (data)=>{
                  self.ctx.socket.emit("res",data);
                }).stderr.on('data',async ()=>{
                });
                stream.end('ls -l\nexit\n');
            });
            
        }else{
             this.ctx.socket.emit("openSSH",false);
        } 
    }
}

module.exports=ssh;